package com.kirayous.task3.service;

import com.kirayous.task3.entity.regionState;
import com.kirayous.task3.utils.TaskOfScheduler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class fetchVirusDataService {


    @Autowired
    TaskOfScheduler scheduler;

    public List<regionState> fetchAllVirusData() throws IOException {



        if (scheduler.getDataList().size()==0) scheduler.updateData();

        return scheduler.getDataList();
    }

    public List<regionState> fetchByConditionVirusData(String state,String country) throws IOException {


        if (state==null&&country!=null)
            return TaskOfScheduler.getDataList()
                    .parallelStream()
                    .filter(regionState -> { return regionState.getCountry().equals(country);})
                    .collect(Collectors.toList());
        else if (state!=null&&country==null)
        {
            return TaskOfScheduler.getDataList()
                    .parallelStream()
                    .filter(regionState -> { return regionState.getState().equals(state);})
                    .collect(Collectors.toList());
        }else if (state!=null&&country!=null)
        {
            return TaskOfScheduler.getDataList()
                    .parallelStream()
                    .filter(regionState -> regionState.getState().equals(state) && regionState.getCountry().equals(country))
                    .collect(Collectors.toList());
        }
        else
            return fetchAllVirusData();
    }

}
